package com.iptv.grape.domain;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.iptv.grape.net.Entity.ChannelData;
import com.iptv.grape.util.AppLog;
import com.iptv.grape.util.ReadJsonFile;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

// 直播频道播放历史
public class ChannelHis{

    // 当前正在播放的频道
    private ChannelRecords curPlayChannel;
    // 最后一次播放的频道
    private ChannelRecords lastPlayChannel;
    // 直播播放历史
    private List<ChannelRecords> list;

    public ChannelRecords getCurPlayChannel() {
        return curPlayChannel;
    }

    // 频道列表更新后 需要同步刷新直播历史数据
    public void syncData(List<Channel> channels){
        Channel curChannel = findChannelDefFirst(channels,curPlayChannel.getChannelID());
        Channel lastChannel = findChannelDefFirst(channels,lastPlayChannel.getChannelID());
        curPlayChannel = curChannel.toChannelRecords();
        lastPlayChannel = lastChannel.toChannelRecords();

        List<ChannelRecords> temList = new ArrayList<ChannelRecords>();
        for(int i = 0; i < list.size(); i++){
            ChannelRecords channelRecords = list.get(i);
            if(isExists(channels,channelRecords.getChannelID())){
                temList.add(channelRecords);
            }
        }
        list = temList;

    }

    private boolean isExists(List<Channel> channelList, String keyID){
        for(int i = 0; i < channelList.size(); i++) {
            Channel channel = channelList.get(i);
            if(channel.getKeyID().equals(keyID)){
                return true;
            }
        }
        return false;
    }

    private Channel findChannelDefFirst(List<Channel> channelList, String keyID){
        for(int i = 0; i < channelList.size(); i++) {
            Channel channel = channelList.get(i);
            if(channel.getKeyID().equals(keyID)){
                return channel;
            }
        }
        return channelList.get(0);
    }


    public void setCurPlayChannel(ChannelRecords curPlayChannel) {
        this.curPlayChannel = curPlayChannel;
    }

    public ChannelRecords getLastPlayChannel() {
        return lastPlayChannel;
    }

    public void setLastPlayChannel(ChannelRecords lastPlayChannel) {
        this.lastPlayChannel = lastPlayChannel;
        this.curPlayChannel = lastPlayChannel;
    }

    public List<ChannelRecords> getList() {
        return list;
    }

    public void setList(List<ChannelRecords> channelHisList) {
        this.list = channelHisList;
    }

    public boolean isEmpty(){
        return this.lastPlayChannel == null;
    }

    public void init(Channel channel){
        this.lastPlayChannel = channel.toChannelRecords();
        this.curPlayChannel = channel.toChannelRecords();
        this.list = new ArrayList<ChannelRecords>();
    }

    public void reSetLastPlayChannel(Channel channel){
        this.lastPlayChannel = channel.toChannelRecords();
        this.curPlayChannel = channel.toChannelRecords();
    }


    public ChannelRecords findChannelRecords(String channelID){
        if(list == null){
            this.list = new ArrayList<ChannelRecords>();
        }
        try {
            for (int i = 0; i < list.size(); i++) {
                ChannelRecords record = list.get(i);
                if (record.getChannelID().equals(channelID)) {
                    return record;
                }
            }
            return null;
        }
        catch (Exception e){
            AppLog.getCurLog().out(e.getMessage());
            return null;
        }
    }

    public void setChannelHis(ChannelRecords channelRecords){

        ChannelRecords record = findChannelRecords(channelRecords.getChannelID());
        if(record == null){
            list.add(channelRecords);
            record = channelRecords;
        }else {
            record.setChannelNo(channelRecords.getChannelNo());
            record.setLogo(channelRecords.getLogo());
            record.setName(channelRecords.getName());
            record.setPlayUrl(channelRecords.getPlayUrl());
            record.addCount();
        }
        this.lastPlayChannel = this.curPlayChannel;
        this.curPlayChannel = record;
        serializeToFile();
    }


    public String toJson(){
        Collections.sort(this.list);
        Collections.reverse(this.list);
        Gson gson = new Gson();
        String json = gson.toJson(this);
        return json;
    }

    // 序列化到文件
    public void serializeToFile(){
        String json = this.toJson();
        ReadJsonFile.writeFile("channelHis.json",json);
        //AppLog.getCurLog().out("序列化直播历史到文件");
    }

    // 反序列化对象
    public static ChannelHis unSerializeToFile(){

        ChannelHis channelHis = new ChannelHis();
        try {
            if(ReadJsonFile.fileExists("channelHis.json")) {
                String json = ReadJsonFile.readFile("channelHis.json");
                channelHis = new Gson().fromJson(json, ChannelHis.class);
            }
            return channelHis;
        }
        catch (Exception ex){
            ex.printStackTrace();
        }
        return channelHis;
    }



}
